package com.fyp.ptma.model;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

/**
 * 
 * @author YahyaArshad
 * 
 */
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "email" }))
@XmlRootElement(name = "owner")
@XmlAccessorType(XmlAccessType.NONE)
public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Long id;
	private List<Group> groups = new ArrayList<Group>();
	private List<Downloader> downloaders = new ArrayList<Downloader>();
	private List<Application> applications = new ArrayList<Application>();

	@XmlElement
	private String email;
	private String firstName;
	private String lastName;
	private String password;
	private String phoneNumber;
	private String role;
	private RegisterStatus registerStatus;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
	@Cascade(value = { CascadeType.ALL })
	public List<Downloader> getDownloaders() {
		return downloaders;
	}

	public void setDownloaders(List<Downloader> downloaders) {
		this.downloaders = downloaders;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public String getRole() {
		return role;
	}

	public User(String email) {
		this.email = email;
	}

	public User() {

	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
	@Cascade(value = { CascadeType.ALL })
	public List<Group> getGroups() {
		return groups;
	}

	public void setRole(String role) {
		role = "ROLE_USER";
		this.role = role;
	}

	@OneToOne(mappedBy = "user")
	@Cascade(value = { CascadeType.ALL })
	public RegisterStatus getRegisterStatus() {
		return registerStatus;
	}

	public void setRegisterStatus(RegisterStatus registerStatus) {
		this.registerStatus = registerStatus;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
	@Cascade(value = { CascadeType.ALL })
	public List<Application> getApplications() {
		return applications;
	}

	public void setApplications(List<Application> applications) {
		this.applications = applications;
	}

	private Date date = new Date(System.currentTimeMillis());

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(unique = true, nullable = false)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(nullable = false)
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@Column(nullable = false)
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Column(nullable = false)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "First name: " + firstName + " :: Last name: " + lastName
				+ " :: Email: " + email;
	}

}
